package com.prp.gwt.client.user.mail;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.vo.MailBoxList;
import com.prp.gwt.client.vo.MailInfo;
import com.prp.gwt.client.vo.MailboxInfo;
import com.prp.gwt.client.vo.NewMail;
import com.prp.gwt.client.widgets.MessageDialogBox;
import com.prp.gwt.client.widgets.mail.InboxMailListPanel;
import com.prp.gwt.client.widgets.mail.MailView;
import com.prp.gwt.client.widgets.mail.NewMailPanel;
import com.prp.gwt.client.widgets.mail.OutboxMailListPanel;
import com.prp.gwt.client.widgets.mail.ReadInMailPanel;
import com.prp.gwt.client.widgets.mail.ReadOutMailPanel;

public class MailListener{

	private static MailListener instance;
	private MailServiceAsync service = GWT.create(MailService.class);

	public static MailListener getInstance() {
		if (instance == null)
			instance = new MailListener();
		return instance;
	}

	public void lookoverInbox(MailboxInfo mailbox) {
		service.getMail(mailbox.getId(), new AsyncCallback<MailInfo>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailInfo result) {
				System.out.println(result.getContent());
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				ReadInMailPanel.getInstance().load(result);
				MailView.getInstance().setBarVisible(false);
				MailView.getInstance().showWidget(MailView.READINMAIL);
			}
		});
	}
	
	public void lookoverOutbox(MailboxInfo mailbox) {
		service.getMail(mailbox.getId(), new AsyncCallback<MailInfo>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailInfo result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				ReadOutMailPanel.getInstance().load(result);
				MailView.getInstance().setBarVisible(false);
				MailView.getInstance().showWidget(MailView.READOUTMAIL);
			}
		});
	}

	public void deleteInbox(MailboxInfo mailbox) {
		service.deleteInbox(mailbox, new AsyncCallback<MailboxInfo>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailboxInfo result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				InboxMailListPanel.getInstance().delete(result);
				MailView.getInstance().showWidget(MailView.INMAILBOX);
			}
		});
	}
	
	public void deleteOutbox(MailboxInfo mailbox) {
		service.deleteOutbox(mailbox, new AsyncCallback<MailboxInfo>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailboxInfo result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				OutboxMailListPanel.getInstance().delete(result);
				MailView.getInstance().showWidget(MailView.OUTMAILBOX);
			}
		});
	}

	public void sendMail(NewMail mail) {
		service.sendMail(mail, new AsyncCallback<Integer>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Integer result) {
				if (result == null || result == -1){
					MessageDialogBox.show("未知错误", "发送失败，请稍后再试！");
					return;
				}if( result == 0){
					MessageDialogBox.show("信息错误", "无此用户！");
					return;
				}
				if( result == 1){
					MessageDialogBox.show("发送提示", "信件已发出！");
					//放在哪里？？？？？
					MailView.getInstance().setBarVisible(true);
					MailView.getInstance().showWidget(MailView.INMAILBOX);
					return;
				}
				MessageDialogBox.show("未知错误", "发送失败，请稍后再试！");
			}
		});

	}

	public void cancelMail() {
		
	}

	/**
	 * 修改ListPanel
	 */
	public void getOutMailList() {
		service.getOutMailList(new AsyncCallback<MailBoxList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailBoxList result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				OutboxMailListPanel.getInstance().load(result);
			}
		});
	}

	public void getInMainList() {
		service.getInMailList(new AsyncCallback<MailBoxList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailBoxList result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				InboxMailListPanel.getInstance().load(result);
			}
		});

	}

	public void getMail(int mailId) {
		service.getMail(mailId, new AsyncCallback<MailInfo>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(MailInfo result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				ReadInMailPanel.getInstance().load(result);
			}
		});
	}

	public void replyMail(NewMail mail) {
		NewMailPanel.getInstance().Load(mail);
		MailView.getInstance().setBarVisible(true);
		MailView.getInstance().showWidget(MailView.NEWMAIL);
	}
	
	/**
	 * 判断用户是否有比identity更大的权力
	 * @return
	 */
	public void canSendMassMail() {
		service.canSendMassMail( new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				MailView.getInstance().disableMassMail();
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if( result == true){
					MailView.getInstance().enableMassMail();
				}else{
					MailView.getInstance().disableMassMail();
				}
			}
		});
	}

	public void sendMassMail(NewMail newMail) {
		service.sendMassMail(newMail, new AsyncCallback<Integer>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Integer result) {
				if (result == null || result == -1){
					MessageDialogBox.show("未知错误", "部分邮件未发出，请稍后再试！");
					return;
				}if( result == 0){
					MessageDialogBox.show("信息错误", "无法获取用户列表！");
					return;
				}
				if( result == 1){
					MessageDialogBox.show("发送提示", "信件已发出！");
					//放在哪里？？？？？
					MailView.getInstance().setBarVisible(true);
					MailView.getInstance().showWidget(MailView.INMAILBOX);
					return;
				}
				MessageDialogBox.show("未知错误", "发送失败，请稍后再试！");
			}
		});
		
	}
	public void openMassMailPanel(){
		service.validateAuth(ClientConstants.ADMIN, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				History.newItem(MailHistoryHandler.MASSMAIL);
			}
		});
	}
}
